﻿layui.use(['layer', 'form'], function () {
    /**
    * @description 加载layer部件
    */
    var layer = layui.layer,
        form = layui.form;

    var index = parent.layer.getFrameIndex(window.name); //当前iframe层的索引

    var map = null;
    var zoom = 13;

    var b_lng, b_lat;

    var point; //= new BMap.Point(0, 0);
    var newpoint; //= new BMap.Point(0, 0);

    function InitialLocation() {
        var fieldData = {
            MapPos: app.getRequest('MapPos'),
            ActionType: "InitialLocation"
        };
        app.get("/Business/MapCoordinates/SetMapPos.ashx", fieldData, function (data) {
            if (data.success) {
                point = new BMap.Point(data.text.split(",")[0], data.text.split(",")[1]);
                newpoint = new BMap.Point(data.text.split(",")[0], data.text.split(",")[1]);
                b_lng = point.lng,
                    b_lat = point.lat;
                GetMap();
                searchTextInit();
            }
        });
    }

    var marker = null;
    var myIcon = null;

    function MapSearch() {
        //本地搜索
        var mapaddess = $("#Mapaddess").val();
        if (mapaddess != '') {
            var local = new BMap.LocalSearch(map, { renderOptions: { map: map } });   //,onSearchComplete: searchComplete
            local.search(mapaddess);
            local.setSearchCompleteCallback(function (results) {   //回调函数
                if (local.getStatus() == BMAP_STATUS_SUCCESS) {
                    map.clearOverlays();
                    var poi = results.getPoi(0);
                    point = poi.point;
                    SetMyMarker(false, '就是这里了！！');
                }
            });
        }
    }

    function SetMyMarker(isNew, title) {
        map.removeOverlay(marker);
        myIcon = new BMap.Icon("/resources/images/location.png", new BMap.Size(30, 30), { offset: new BMap.Size(15, 15) });
        if (isNew)
            point = newpoint;
        marker = new BMap.Marker(point, { icon: myIcon });      // 创建标注
        map.addOverlay(marker);

        marker.enableDragging(true);    // 设置标注可拖拽

        marker.addEventListener("dragstart", function (e) {
            $(".BMapLabel").hide();
        });

        marker.addEventListener("dragend", function (e) {
            //$(".BMapLabel").show();
        });

        marker.onmouseup = function () {
            point = marker.getPosition();
            SetMapPos(point.lng, point.lat);
            SetMyLabel("就这里了！！");
        };
        SetMyLabel(title);

    }

    function SetMyLabel(title) {
        var label = new BMap.Label(title, { offset: new BMap.Size(30, 30) });
        marker.setLabel(label);
    }

    $(document).ready(function () {
        InitialLocation();
    });

    function searchTextInit() {
        $("#Mapaddess").keydown(function (e) {
            if (e.keyCode == 13) { //若按了enter键 则触发搜索事件
                e.preventDefault();
                MapSearch();
            }
        }).focus(function () {
            $(".search_left_label").hide();
        }).blur(function () {
            if ($("#Mapaddess").val() == "") {
                $(".search_left_label").show();
            }
        });

        if ($("#Mapaddess").val() != "") {
            $(".search_left_label").hide();
        }
    }

    var saveIndex = -1;
    function GetMap() {
        map = new BMap.Map("myMap");
        map.centerAndZoom(point, zoom);
        var menu = new BMap.ContextMenu();
        var txtMenuItem = [{
            text: '放大',
            callback: function () { map.zoomIn() }
        }, {
            text: '缩小',
            callback: function () { map.zoomOut() }
        }];
        map.addEventListener("click", function (e) {
            point = new BMap.Point(e.point.lng, e.point.lat);
            marker.setPosition(point);
            SetMyMarker(false, "就这里了！！"); //标注
            SetMapPos(e.point.lng, e.point.lat);
        });

        for (var i = 0; i < txtMenuItem.length; i++) {
            menu.addItem(new BMap.MenuItem(txtMenuItem[i].text, txtMenuItem[i].callback, 100));
        }
        map.addContextMenu(menu);
        SetMyMarker(true, "拖动此图标到精确位置"); //标注

        map.enableScrollWheelZoom();                  // 启用滚轮放大缩小。
        map.enableKeyboard();                         // 启用键盘操作。
        map.addControl(new BMap.NavigationControl());               // 添加平移缩放控件
        map.addControl(new BMap.ScaleControl());                    // 添加比例尺控件 
    }

    //debugger;
    function SetMapPos(lng, lat) {
        b_lng = lng;
        b_lat = lat;
    }

    //确定
    form.on('submit(save)', function (data) {
        parent.window.layer.selectMap(b_lng + "," + b_lat);
        parent.layer.close(index); //执行关闭
        return false;
    });

    //取消
    $('#btnCancel').on('click', function () {
        parent.layer.close(index); //执行关闭
    });

    // 搜索
    $('#MapSearch').on('click', function () {
        MapSearch();
    });
});
